package baseFx.tests.common;

import baseFx.common.Dynamic;
import org.junit.Assert;
import org.junit.Test;

import java.util.Date;
import java.util.UUID;

public class DynamicTests {
    @Test
    public void 不能读取私有字段() {
        Dynamic dynamic = new Dynamic() {
            private String id = UUID.randomUUID().toString();
        };
        Assert.assertNull(dynamic.get("id"));
        Assert.assertEquals(dynamic.toMap().size(), 0);
    }

    @Test
    public void 可以读取非私有字段() {
        Dynamic dynamic = new Dynamic() {
            String id = UUID.randomUUID().toString();
            public String name = UUID.randomUUID().toString();
        };
        Assert.assertNull(dynamic.get("id"));
        Assert.assertNotNull(dynamic.get("name"));
        Assert.assertEquals(dynamic.toMap().size(), 1);
    }

    @Test
    public void 解析lombok风格对象() {
        Lombok model = new Lombok();
        Dynamic dynamic = new Dynamic(model);
        Assert.assertEquals(dynamic.get("id"), 1);
        Assert.assertEquals(dynamic.get("name"), "abc");
        Assert.assertEquals(dynamic.toMap().size(), 2);
    }

    public static class Lombok {
        public Lombok id(Integer value) {
            return this;
        }

        public Integer id() {
            return 1;
        }

        public Lombok name(String value) {
            return this;
        }

        public String name() {
            return "abc";
        }

        public Date time() {
            return new Date(0);
        }
    }
}
